package com;
/*

                  写字楼里写字间，写字间里程序员；
                  程序人'猿'写程序，又拿程序换酒钱。
                  酒醒只在网上坐，酒醉还来网下眠；
                  酒醉酒醒日复日，网上网下年复年。
                  但愿老死电脑间，不愿鞠躬老板前；
                  奔驰宝马贵者趣，公交自行程序员。
                  别人笑我忒疯癫，我笑自己命太贱；
                  不见满街漂亮妹，哪个归得程序'猿'？
━━━━━━神兽出没━━━━━━
 ┏┓   ┏┓
┏┛┻━━━┛┻┓
┃       ┃
┃   ━   ┃
┃ ┳┛ ┗┳ ┃
┃       ┃
┃   ┻   ┃
┃       ┃
┗━┓   ┏━┛
  ┃   ┃                    神兽保佑, 永无BUG!
  ┃   ┃       Code is far away from bug with the animal protecting
  ┃   ┗━━━┓
  ┃       ┣┓
  ┃       ┏┛
  ┗┓┓┏━┳┓┏┛
   ┃┫┫ ┃┫┫
   ┗┻┛ ┗┻┛
 */

import com.ehcache.EhcacheManager;
import com.scheduler.ScheduledTasks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by 石头哥哥 on 6/13/16.
 * <p>
 * common
 * <p>
 * com.ehcache
 * <p>
 * comments:
 */
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Component
@ImportResource("classpath:spring-task.xml")
public class EhcacheTest {

    /**
     * ehcache  ref
     */
    @Resource
    private EhcacheManager ehcacheManager;

    /**
     * log
     */
    private static final Logger log = LoggerFactory.getLogger(EhcacheTest.class);


    @Resource
    ScheduledTasks scheduledTasks;

    /**
     * support protobuf http message converter
     * @return
     */
    @Bean
    public ProtobufHttpMessageConverter protobufHttpMessageConverter(){
        return new ProtobufHttpMessageConverter();
    }

    @Bean
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        return new MappingJackson2HttpMessageConverter();
    }


    /**
     * @param args
     */
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(EhcacheTest.class,args);
        EhcacheTest ehcacheTest = (EhcacheTest) applicationContext.getBean("ehcacheTest");//byName
        ehcacheTest.ehcacheManager.put("username", 100, "这是测试字符串");
        ehcacheTest.ehcacheManager.put("username", 101, applicationContext);
        String test_str = String.valueOf(ehcacheTest.ehcacheManager.get("username", 100));
        ConfigurableApplicationContext applicationContext0 =
                (ConfigurableApplicationContext) ehcacheTest.ehcacheManager.get("username", 101);
        if (applicationContext0.equals(applicationContext)) {//内存地址一致,同一个对象
            log.debug("applicationContext0==applicationContext");
        } else {
            log.debug("applicationContext not equal applicationContext");
        }
        ehcacheTest.ehcacheManager.remove("username", 100);
        ehcacheTest.ehcacheManager.remove("username", 101);
        log.debug("test info :{}", test_str);
    }


}
